package com.skyjing.askit

import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.os.PowerManager
import android.provider.Settings
import androidx.annotation.RequiresApi

object AsRequstWhiteList {

    const val HUAWEI = "HUAWEI"
    const val XIAOMI = "XIAOMI"
    const val OPPO = "OPPO"
    const val VIVO = "VIVO"
    const val MEIZU = "MEIZU"
    const val SAMSUNG = "SAMSUNG"
    const val LETV = "LETV"
    const val SMARTISAN = "SMARTISAN"

    /**
     * 通过以下代码申请加入电池白名单
     */
    @RequiresApi(api = Build.VERSION_CODES.M)
    @JvmStatic
    fun requestIgnoreBatteryOptimizations() {
        try {
            val intent =
                Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS)
            intent.data = Uri.parse("package:" + AsTool.getPageName())
            intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
            AsTool.getApp()?.startActivity(intent)
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    /**
     * 判断我们的应用是否在电池白名单中
     *
     * @return
     */
    @RequiresApi(api = Build.VERSION_CODES.M)
    @JvmStatic
    fun isIgnoringBatteryOptimizations(): Boolean {
        var isIgnoring = false
        val powerManager =
            AsTool.getApp()?.getSystemService(Context.POWER_SERVICE) as PowerManager?
        if (powerManager != null) {
            isIgnoring = powerManager.isIgnoringBatteryOptimizations(AsTool.getPageName())
        }
        return isIgnoring
    }

    fun isBrand() {
        Build.BRAND.let {
            when (it.toLowerCase()) {
                "huawei",
                "honor" -> {
                    goHuaweiSetting()
                }
                "xiaomi"->{
                    goXiaomiSetting()
                }
                "oppo"->{
                    goOPPOSetting()
                }
                "vivo"->{
                    goVIVOSetting()
                }
                "meizu"->{
                    goMeizuSetting()
                }
                "samsung"->{
                    goSamsungSetting()
                }
                "letv"->{
                    goLetvSetting()
                }
                "smartisan"->{
                    goSmartisanSetting()
                }
                else -> ""
            }
        }
    }

    /**
     * 跳转华为手机管家的启动管理页
     * 操作步骤：应用启动管理 -> 关闭应用开关 -> 打开允许自启动
     */
    private fun goHuaweiSetting() {
        try {
            showActivity(
                "com.huawei.systemmanager",
                "com.huawei.systemmanager.startupmgr.ui.StartupNormalAppListActivity"
            )
        } catch (e: java.lang.Exception) {
            showActivity(
                "com.huawei.systemmanager",
                "com.huawei.systemmanager.optimize.bootstart.BootStartActivity"
            )
        }
    }

    /**
     * 操作步骤：授权管理 -> 自启动管理 -> 允许应用自启动
     */
    private fun goXiaomiSetting() {
        try {
            showActivity(
                "com.miui.securitycenter",
                "com.miui.permcenter.autostart.AutoStartManagementActivity"
            )
        } catch (e: java.lang.Exception) {
        }
    }

    /**
     * 操作步骤：权限隐私 -> 自启动管理 -> 允许应用自启动
     */
    private fun goOPPOSetting() {
        try {
            showActivity("com.coloros.phonemanager")
        } catch (e1: java.lang.Exception) {
            try {
                showActivity("com.oppo.safe")
            } catch (e2: java.lang.Exception) {
                try {
                    showActivity("com.coloros.oppoguardelf")
                } catch (e3: java.lang.Exception) {
                    showActivity("com.coloros.safecenter")
                }
            }
        }
    }

    /**
     * 操作步骤：权限管理 -> 自启动 -> 允许应用自启动
     */
    private fun goVIVOSetting() {
        try {
            showActivity("com.iqoo.secure")
        } catch (e: java.lang.Exception) {
        }
    }

    /**
     * 操作步骤：权限管理 -> 后台管理 -> 点击应用 -> 允许后台运行
     */
    private fun goMeizuSetting() {
        try {
            showActivity("com.meizu.safe")
        } catch (e: java.lang.Exception) {
        }
    }

    /**
     * 操作步骤：自动运行应用程序 -> 打开应用开关 -> 电池管理 -> 未监视的应用程序 -> 添加应用
     */
    private fun goSamsungSetting() {
        try {
            showActivity("com.samsung.android.sm_cn")
        } catch (e: java.lang.Exception) {
            showActivity("com.samsung.android.sm")
        }
    }

    /**
     * 操作步骤：自启动管理 -> 允许应用自启动
     */
    private fun goLetvSetting() {
        try {
            showActivity(
                "com.letv.android.letvsafe",
                "com.letv.android.letvsafe.AutobootManageActivity"
            )
        } catch (e: java.lang.Exception) {
        }
    }

    /**
     * 操作步骤：权限管理 -> 自启动权限管理 -> 点击应用 -> 允许被系统启动
     */
    private fun goSmartisanSetting() {
        try {
            showActivity("com.smartisanos.security")
        } catch (e: java.lang.Exception) {
        }
    }


    /***********************************************************************************************
     * 系统后台管理
     */
    /**
     * 跳转到指定应用的首页
     */
    private fun showActivity(packageName: String) {
        val intent: Intent? =
            AsTool.getApp()?.packageManager?.getLaunchIntentForPackage(packageName)
        if (intent != null)
            AsTool.getApp()?.startActivity(intent)
    }

    /**
     * 跳转到指定应用的指定页面
     */
    private fun showActivity(
        packageName: String,
        activityDir: String
    ) {
        val intent = Intent()
        intent.component = ComponentName(packageName, activityDir)
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        AsTool.getApp()?.startActivity(intent)
    }
}